import java.io.{BufferedReader, InputStreamReader}
import java.sql.DriverManager
import java.util.Scanner

import org.apache.ignite.{IgniteJdbcThinDriver, Ignition}
/**
  * @author dinghao 
  * @create 2021-09-09 15:09 
  * @message
  */
object EmbededTest {
  def main(args: Array[String]): Unit = {
    val scanner = new Scanner(System.in)
    Ignition.start()
    println("按任意键后回车继续:")
    scanner.next()
    IgniteJdbcThinDriver.register()
    val url = "jdbc:ignite:thin://192.168.1.240/"
    val conn = DriverManager.getConnection(url)
    val stat = conn.createStatement()

    System.out.println("建表City：")
    stat.executeUpdate("CREATE TABLE City (" +
      " id LONG PRIMARY KEY, name VARCHAR) " +
      " WITH \"template=replicated\"")
    System.out.println("建表成功")
    println("按任意键后回车继续:")
    scanner.next()

    System.out.println("插入数据（1，nanjin）")
    val stat2 = conn.prepareStatement(
      "INSERT INTO City (id, name) VALUES (?, ?)")
    stat2.setLong(1, 1L)
    stat2.setString(2, "nanjin")
    stat2.executeUpdate()
    System.out.println("插入成功，查询数据：")
    println("按任意键后回车继续:")
    scanner.next()
    val stat7 = conn.createStatement()
    val resultSet1 = stat7.executeQuery("select * from City")
    if (resultSet1.next()) {
      System.out.println(resultSet1.getLong(1) +
        "," + resultSet1.getString(2))
    }

    System.out.println("修改数据为：（1，nanjingai）")
    val stat6 = conn.prepareStatement(
      "update City set name = 'nanjingai'")
    stat6.executeUpdate()

    System.out.println("修改成功，查询数据为：")
    val stat3 = conn.createStatement()
    val resultSet = stat3.executeQuery("select * from City")
    if (resultSet.next()) {
      System.out.println(resultSet.getLong(1) +
        "," + resultSet.getString(2))
    }
    println("按任意键后回车继续:")
    scanner.next()

    System.out.println("清空数据库数据：")
    val stat4 = conn.prepareStatement(
      "delete from City")
    stat4.executeUpdate()

    System.out.println("删除成功，查询数据为：")

    val stat5 = conn.createStatement()
    val resultSet5 = stat5.executeQuery("select * from City")
    if (resultSet5.next()) {
      System.out.println(resultSet5.getLong(1) +
        "," + resultSet5.getString(2))
    }else{
      System.out.println("无数据")
    }
    println("按任意键后回车继续:")
    scanner.next()

    stat3.close()
    stat2.close()
    stat4.close()
    stat5.close()
    stat6.close()
    stat7.close()
    stat.close()
    conn.close()
    Ignition.stop(true)
  }
}
